import request from '@/util/request'
import wx from 'weixin-js-sdk'

// get openid
export function getOpenid(data) {
  return request({
    url: '/apis/wx/user/get-openid',
    method: 'post',
    data
  })
}

// get
export function get(data) {
  return request({
    url: '/apis/wx/user/get',
    method: 'post',
    data
  })
}

// ------------------------------------------------------------

const payPrepareWxH5 = (data) => {
  return request({
    url: '/apis/pay/pay-wx-h5/prepare',
    method: 'post',
    data
  })
}

const payPrepareWx = (data) => {
  return request({
    url: '/apis/pay/pay-wx/prepare',
    method: 'post',
    data
  })
}

// prepare pay
const payPrepare = (data) => {
  return request({
    url: '/apis/pay/pay/prepare',
    method: 'post',
    data
  })
}

/* eslint-disable */
export function jsApiPay_Wx_H5(data) {
  return new Promise((resolve, reject) => {
    payPrepareWxH5(data).then(response => {

      // const data = response.data.result.rc_Result
      // const signature = response.data.signature

      const { data } = response

      wx.config({
        debug: false,
        appId: data.appId,
        timestamp: data.timestamp,
        nonceStr: data.nonceStr,
        signature: data.signature,
        jsApiList: ['chooseWXPay']
      })
      wx.chooseWXPay(
        {
          timestamp: data.timestamp,
          nonceStr: data.nonceStr,
          package: data.package,
          // signType: 'RSA',
          signType: 'MD5',
          // paySign: data.paySign,
          paySign: data.sign,
          success: (data) => {
            resolve()
          },
          fail: (data) => {
            reject()
          },
          cancel: () => {
            reject()
          }
        }
      )
      // console.log(response)
    }).catch(err => {
      console.log(err.message)
      reject(err)
    })
  })
}

/* eslint-disable */
export function jsApiPay_Wx(data) {
  return new Promise((resolve, reject) => {
    payPrepareWx(data).then(response => {

      // const data = response.data.result.rc_Result
      // const signature = response.data.signature

      const { data } = response

      wx.config({
        debug: false,
        appId: data.appId,
        timestamp: data.timestamp,
        nonceStr: data.nonceStr,
        signature: data.signature,
        jsApiList: ['chooseWXPay']
      })
      wx.chooseWXPay(
        {
          timestamp: data.timestamp,
          nonceStr: data.nonceStr,
          package: data.package,
          // signType: 'RSA',
          signType: 'MD5',
          // paySign: data.paySign,
          paySign: data.sign,
          success: (data) => {
            resolve()
          },
          fail: (data) => {
            reject()
          },
          cancel: () => {
            reject()
          }
        }
      )
      // console.log(response)
    }).catch(err => {
      console.log(err.message)
      reject(err)
    })
  })
}

/* eslint-disable */
export function jsApiPay(data) {
  return new Promise((resolve, reject) => {
    payPrepare(data).then(response => {

      const data = response.data.result.rc_Result
      const signature = response.data.signature

      wx.config({
        debug: false,
        appId: data.appId,
        timestamp: data.timeStamp,
        nonceStr: data.nonceStr,
        signature: signature,
        jsApiList: ['chooseWXPay']
      })
      wx.chooseWXPay(
        {
          timestamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: 'RSA',
          paySign: data.paySign,
          success: (data) => {
            resolve()
          },
          fail: (data) => {
            reject()
          },
          cancel: () => {
            reject()
          }
        }
      )
      // console.log(response)
    }).catch(err => {
      console.log(err.message)
      reject(err)
    })
  })
}
